1 Libraries

library(gapminder) # Gapminder data

library(ggplot2) # beautiful graphs

library(rayshader) # WOW

library(rgl) # 3D graphics
## Warning: package 'rgl' was built under R version 4.0.5
library(knitr)

# knit_hooks$set(rgl = hook_rgl)

2 Data

data(gapminder)

3 ggplot

p0 <- ggplot(gapminder, 
            aes(gdpPercap, 
                lifeExp)) +
  stat_density_2d(aes(fill = stat(nlevel)), 
                  geom = "polygon",
                  n = 100, 
                  bins = 10, 
                  contour = TRUE) +
  labs(title = "Life Expectancy by GDP",
       x = "GDP",
       y = "Life Expectancy") +
  theme_minimal() +
  scale_fill_viridis_c()

p0

4 rayshader

# knit_hooks$set(rgl = hook_rgl)

# rgl::rgl.close()

plot_gg(p0, 
        multicore = TRUE, 
        width = 5, 
        height = 5, 
        scale = 250)

rgl::rglwidget()